package com.snailvr.tv.provider;


import android.util.Log;

import com.snail.readyvr.util.EventBus;
import com.snail.readyvr.util.Utils;
import com.snailvr.tv.Definition;
import com.snailvr.tv.event.LoadFailureEvent;
import com.snailvr.tv.network.RetrofitFactory;
import com.snailvr.tv.network.entity.ChannelListResponBean;
import com.snailvr.tv.network.service.ChannelListService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/9/2.
 */
public class ChanelListProvider implements IChanelsProvider {

    private ChannelListService mChannelListService;
    private ChannelListResponBean mChannelListResponBean;

    public ChanelListProvider() {
        mChannelListService = RetrofitFactory.getmChannelsService();
    }

    @Override
    public void getChanels(int page_size, int page,String tag, boolean isLoadMore, boolean needClear) {
        getChannelsFromServer(page_size, page, tag);

    }

    private void getChannelsFromServer(int page_size, int page, String tag) {


        Call<ChannelListResponBean> call = mChannelListService.getChannelList(Utils.sessionid,Utils.token,page_size,page,tag);

        call.enqueue(new Callback<ChannelListResponBean>() {
            @Override
            public void onResponse(Call<ChannelListResponBean> call, Response<ChannelListResponBean> response) {
                if (response.body()!=null) {
                    mChannelListResponBean = response.body();
                    if (mChannelListResponBean.getCode() == 0) {
                        Log.i("App","message:" + mChannelListResponBean.getMessage() + "item size:" +mChannelListResponBean.getData().getList().size());

                        EventBus.getDefault().post(Definition.BusTag.CHANNLE_LIST_TAG,mChannelListResponBean);
                    } else {
                        EventBus.getDefault().post(Definition.BusTag.CHANNLE_LIST_TAG,new LoadFailureEvent(mChannelListResponBean.getCode(),mChannelListResponBean.getMessage()));
                    }
                }
            }

            @Override
            public void onFailure(Call<ChannelListResponBean> call, Throwable t) {
                Log.d("App", "onFailure!");
                EventBus.getDefault().post(Definition.BusTag.CHANNLE_LIST_TAG, new LoadFailureEvent(0, "频道列表请求失败"));
            }
        });
    }
}
